package com.neuedu.handler;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.neuedu.po.AssiantPO;
import com.neuedu.po.ImagePO;
import com.neuedu.po.RentPO;
import com.neuedu.po.ViewRentPO;
import com.neuedu.service.RentService;
import com.neuedu.util.ConstantUtil;
import com.neuedu.vo.RentListSearchVO;
import com.neuedu.vo.RentListVO;

@RestController
@RequestMapping("/rent")
public class RentHandler {
	
	@Autowired
	private RentService rs;
	
	@RequestMapping("/rent_list")
	public List<RentListVO> getRentList(RentListSearchVO vo){
		return rs.getRentList(vo);
	}
	
	@RequestMapping("/rent_count")
	public Long getRentListCount(RentListSearchVO vo) {
		return rs.getRentListCount(vo);
	}
	
	@RequestMapping("/add_rent")
	
	public Integer addRendInfo(HttpServletRequest request,
			MultipartFile[] photos1, MultipartFile[] photos2, 
			Integer[] assiant, Integer couid, Integer fixid,
			Integer htype, Integer agentid, String title,
			Double price, Integer rnum, Integer tnum,
			Integer wnum, Double area, Integer floor,
			Integer totalfloor, String address, String intro) throws Exception {
		//基本信息
		RentPO po = new RentPO();
		po.setCouid(couid);
		po.setFixid(fixid);
		po.setHtype(htype);
		po.setAgentid(agentid);
		po.setTitle(title);
		po.setPrice(price);
		po.setRnum(rnum);
		po.setTnum(tnum);
		po.setWnum(wnum);
		po.setArea(area);
		po.setFloor(totalfloor);
		po.setTotalfloor(totalfloor);
		po.setAddress(address);
		po.setIntro(intro);
		po.setStatus(2);
		//配套信息
		List<Integer> pt = new ArrayList<Integer>();
		if(assiant != null) {
			for(int p : assiant) {
				pt.add(p);
			}
		}
		//照片信息
		List<String> list1 = new ArrayList<String>();
		if(photos1 != null) {
			for(MultipartFile f : photos1) {
	            String old_name = f.getOriginalFilename(); //原始文件名
	            String ext_name = old_name.substring(old_name.lastIndexOf(".")); //扩展名
	            String new_name = UUID.randomUUID().toString()+ext_name; //利用UUID生成新文件名
	            String path = request.getRealPath(ConstantUtil.PHOTO_PATH1);//确定上传位置
	            //如果路径不存在，则创建路径
	            File fx = new File(path);
	            if(!fx.exists()) {
	                fx.mkdirs();
	            }
	            //在路径下创建文件
	            File n = new File(fx, new_name);
	            f.transferTo(n);
	            list1.add(ConstantUtil.PHOTO_PATH1+"/"+new_name);
	        }
		}
		//户型图
		List<String> list2 = new ArrayList<String>();
		if(photos1 != null) {
			for(MultipartFile f : photos2) {
	            String old_name = f.getOriginalFilename(); //原始文件名
	            String ext_name = old_name.substring(old_name.lastIndexOf(".")); //扩展名
	            String new_name = UUID.randomUUID().toString()+ext_name; //利用UUID生成新文件名
	            String path = request.getRealPath(ConstantUtil.PHOTO_PATH2);//确定上传位置
	            File fx = new File(path);
	            if(!fx.exists()) {
	                fx.mkdirs();
	            }
	            File n = new File(fx, new_name);
	            f.transferTo(n);
	            list2.add(ConstantUtil.PHOTO_PATH2+"/"+new_name);
	        }
		}
		rs.insertNewRentalInfo(po, list1, list2, pt);
		return 1;
	}
	
	@RequestMapping("/view_rent")
	public ViewRentPO getRentViewById(Integer rentid){
		System.out.println(rs.getRentViewById(rentid));
		return rs.getRentViewById(rentid);
	}

	@RequestMapping("/view_rentassiant")
	public List<AssiantPO> getRentAssisantById(Integer rentid){
		return rs.getRentAssisantById(rentid);
	}
	
	@RequestMapping("/view_rentphoto")
	public List<ImagePO> getRentPhotoById(Integer rentid){
		return rs.getRentPhotoById(rentid);
	}
}
